package com.example.object;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;


public class Main7 {

    public static void main(String[] args) {
        log(isSocketAliveUitlitybyCrunchify("localhost", 80));

        log(isSocketAliveUitlitybyCrunchify("localhost", 8080));

        log(isSocketAliveUitlitybyCrunchify("localhost", 8081));

        log(isSocketAliveUitlitybyCrunchify("runoob.com", 80));

        log(isSocketAliveUitlitybyCrunchify("runoob.com", 443));

        log(isSocketAliveUitlitybyCrunchify("runoob.com", 81));
    }

    public static boolean isSocketAliveUitlitybyCrunchify(String hostName, int port) {
        boolean isAlive = false;

        SocketAddress socketAddress = new InetSocketAddress(hostName, port);
        Socket socket = new Socket();

        int timeout = 2000;

        log("hostName: " + hostName + ", port: " + port);
        try {
            socket.connect(socketAddress, timeout);
            socket.close();
            isAlive = true;

        } catch (SocketTimeoutException exception) {
            System.out.println("SocketTimeoutException " + hostName + ":" + port + ". " + exception.getMessage());
        } catch (IOException exception) {
            System.out.println(
                    "IOException - Unable to connect to " + hostName + ":" + port + ". " + exception.getMessage());
        }
        return isAlive;
    }

    private static void log(String string) {
        System.out.println(string);
    }

    private static void log(boolean isAlive) {
        System.out.println("是否真正在使用: " + isAlive + "\n");
    }

}